package com.product.app.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.product.app.service.CategoryService;
import com.product.app.service.ProductImageService;
import com.product.app.service.ProductService;
import com.product.app.service.PropertyService;
import com.tamll.Type.ProductDetailsType;
import com.tamll.bean.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
public class ProductController {
    @Autowired
    private ProductService productService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private ProductImageService productImageService;
    @Autowired
    private PropertyService propertyService;

    @GetMapping("/ProductInformation")
    public Page<Product> ProductInformation1(@RequestParam(defaultValue = "0") Integer pageNumber, @RequestParam(defaultValue = "10") Integer num){
        Page<Product> list = productService.getList(pageNumber,num);
        System.out.println(list.toString());
        return list;
    }

    @GetMapping("/Ca")
    public Page<Category> Ca(@RequestParam(defaultValue = "0") Integer pageNumber, @RequestParam(defaultValue = "20") Integer num){
        Page<Category> list = categoryService.getList(pageNumber,num);
        System.out.println(list.toString());
        return list;
    }

    @GetMapping("productPage")
    public String ProductPage(){
        return "admin/productManagePage";
    }

    @GetMapping("productDetails")
    public String productDetails1(){
        return "admin/include/productDetails";
    }

    @GetMapping("productData")
    public ProductDetailsType productDetails(@PathVariable Integer pid/* 产品ID */){
        Product product = productService.get(pid);
        List<Product> productList=new ArrayList<>();
         productList.add(product);
        System.out.println(productList.toString());

        Integer productCategoryId = product.getProductCategoryId();
        Category category = categoryService.get(productCategoryId);
        List<Category> categoryList=new ArrayList<>();
        categoryList.add(category);

        Property property = propertyService.get(category.getCategoryId());
        List<Property> propertyList=new ArrayList<>();
        propertyList.add(property);

        ProductImage productImage = productImageService.get(product.getProductId());
        List<ProductImage> productImageList=new ArrayList<>();
        productImageList.add(productImage);

        ProductDetailsType productDetailsType=new ProductDetailsType();
        productDetailsType.setProductList(productList);
        productDetailsType.setCategoryList(categoryList);
        productDetailsType.setPropertyList(propertyList);
        productDetailsType.setProductImageList(productImageList);

        return productDetailsType;
    }

}
